/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DataConverters.DataFormatters.formattersimpl;

import DataConverters.DataFormatters.AbstractDataFormatter;
import DataConverters.exceptions.DataConvertException;
import DataConverters.interfaces.IConvertable;
import DataConverters.interfaces.IConvertableFile;
import DataConverters.interfaces.dataformulaobject.intefaces.Cell;
import DataConverters.interfaces.dataformulaobject.intefaces.CellType;
import DataConverters.interfaces.dataformulaobject.intefaces.NumberFormulaCell;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.SortedSet;

/**
 *
 * @author Nobody
 */
public class CSVFormatter extends AbstractDataFormatter {

    private String delimiter;
    private String endOfLine;

    public CSVFormatter(SortedSet<Cell> list) {
        super(list);
    }

    public CSVFormatter() {
    }

    public CSVFormatter(HashMap<String, Object> properties) {
        super(properties);
    }

    private void extractParametersFromMap() throws DataConvertException {
        delimiter = properties.get("delimiter").toString();
        endOfLine = properties.get("endLine").toString();
        if (delimiter == null || endOfLine == null) {
            throw new DataConvertException("To use CSV Formatter \"delimiter\" and \"endLine\" property in properties map" + " must be specified");
        }
    }

    @Override
    public void format(IConvertable source) throws DataConvertException {
        IConvertableFile fileSource = (IConvertableFile) source;
        BufferedWriter buff = null;
        try {

            buff = new BufferedWriter(new FileWriter(fileSource.asFile()));
            try {
                extractParametersFromMap();
                int currentRow = 0;
                boolean begin = true;
                for (Cell object : list) {
                    if (object.getRow() != currentRow) {
                        ++currentRow;
                        buff.write(endOfLine);
                        begin = true;
                    }
                    if (begin == true) {
                        begin = false;
                    } else {
                        buff.write(delimiter);
                    }
                    if (object.getType().equals(CellType.NUMBER)) {
                        buff.write((object.getContents()));
                    } else if (object.getType().equals(CellType.NUMBER_FORMULA)) {
                        NumberFormulaCell cellformula = (NumberFormulaCell) object;
                        buff.write(String.valueOf(cellformula.getValue()));
                    } else {
                        buff.write(String.valueOf(0.0d));
                    }
                }
                buff.close();
            } catch (IOException ex) {
                buff.close();
                throw new DataConvertException("error in opening file : " + fileSource.asFile(), ex);
            }
        } catch (IOException ex) {
            throw new DataConvertException("error in writing to  file : " + fileSource.asFile(), ex);
        }
    }
}
